﻿/*
*功能：时间按钮调置函数
*/
//使用格式setTime="time"  设置日期及时间
//使用格式setTime 设置日期
function CreateTimeBtn(el) {
    var sElementName = el.id;
    if (!sElementName)
        sElementName = el.name;

    if (el.tagName == "INPUT" && !document.getElementById("__timebtn$_" + sElementName)) {
        el.style.height = "18px";
        var sclassName = "";
        var sonclick = "";
        var sHTML = "";
        //在原来的基础上调宽18象素，约5mm
        var iWidth = parseFloat(el.style.width);
        var sWidth = el.style.width + "";
        if (!isNaN(iWidth)) {
            if (sWidth.indexOf("mm") > 0) {
                el.style.width = (iWidth + 5) + "mm";
            }
            else if (sWidth.indexOf("cm") > 0) {
                el.style.width = (iWidth + 0.5) + "cm";
            }
            else if (sWidth.indexOf("%") < 0) {
                el.style.width = (iWidth + 18) + "px";
            }
        }
        if (el.setTime == "time") {
            if (isNaN(iWidth)) el.style.width = "134px";
            sonclick = "showDateAndTime('" + sElementName + "')";
        }
        else {
            if (isNaN(iWidth)) el.style.width = "105px";
            sonclick = "showDate('" + sElementName + "')";
        }

        //如果控件本身指定了想要的width(通过FixWidth指定)
        if (el.FixWidth) {
            el.style.width = el.FixWidth;
        }

        //初始值获取当前时间
        if (typeof (el.GetNowTime) != "undefined") {
            if (el.value == "") {
                var bSetValue = true;
                if (typeof (g_DocObject) != "undefined") {
                    bSetValue = g_DocObject.IsNewDoc()
                }
                if (bSetValue) {
                    //时间从服务器端获取
                    if (el.setTime == "time")
                        el.value = Now(true);
                    else
                        el.value = ToDate(true);
                }
            }
        }

        sHTML = '<span class="spanTime  PHE"><input id="__timebtn$_' + sElementName + '" type="button" value="6" class="btntime" onclick="' + sonclick + '"/></span>';
        el.insertAdjacentHTML("afterEnd", sHTML);
    }
}

function showDate(sElementName) {
    var oIs = document.getElementsByName(sElementName);
    if (oIs.length > 0) {
        var sInitTime = oIs[0].value;
        if (sInitTime == "") {
            sInitTime = Now(true);
            if (typeof (oIs[0].GotoLastMonth) != "undefined" || typeof (oIs[0].ShowLastMonth) != "undefined") {
                sInitTime = AddMonths(sInitTime, -1);
                sInitTime = sInitTime.LeftOfLast("-") + "-01";
            }
            else if (typeof (oIs[0].GotoNextMonth) != "undefined" || typeof (oIs[0].ShowNextMonth) != "undefined") {
                sInitTime = AddMonths(sInitTime, 1);
                sInitTime = sInitTime.LeftOfLast("-") + "-01";
            }
        }

        if (sInitTime != "") {
            sInitTime = sInitTime.replace(/\-/g, "/");
            sInitTime = sInitTime.replace("年", "/").replace("月", "/").replace("日", "");
            sInitTime = sInitTime.replace("点", ":").replace("小时", ":").replace("分钟", ":");
            sInitTime = sInitTime.replace("时", ":").replace("分", ":").replace("秒", "");
        }

        var oInitTime = new Date(sInitTime);
        getDateString(oIs[0], oInitTime, false);
    }
}

function showDateAndTime(sElementName) {
    var oIs = document.getElementsByName(sElementName);
    if (oIs.length > 0) {
        var sInitTime = oIs[0].value;
        if (sInitTime == "") {
            sInitTime = Now(true);
            if (typeof (oIs[0].GotoLastMonth) != "undefined" || typeof (oIs[0].ShowLastMonth) != "undefined") {
                sInitTime = AddMonths(sInitTime, -1);
                var sTimePart = sInitTime.RightOfLast(" ");
                sInitTime = sInitTime.LeftOfLast("-") + "-01 " + sTimePart;
            }
            else if (typeof (oIs[0].GotoNextMonth) != "undefined" || typeof (oIs[0].ShowNextMonth) != "undefined") {
                sInitTime = AddMonths(sInitTime, 1);
                var sTimePart = sInitTime.RightOfLast(" ");
                sInitTime = sInitTime.LeftOfLast("-") + "-01 " + sTimePart;
            }
        }

        if (sInitTime != "") {
            sInitTime = sInitTime.replace(/\-/g, "/");
            sInitTime = sInitTime.replace("年", "/").replace("月", "/").replace("日", "");
            sInitTime = sInitTime.replace("点", ":").replace("小时", ":").replace("分钟", ":");
            sInitTime = sInitTime.replace("时", ":").replace("分", ":").replace("秒", "");
        }

        var oInitTime = new Date(sInitTime);
        getDateString(oIs[0], oInitTime, true);
    }
}

/************************************************/

function getDateString(Obj, dtCur, bGetTime) {
    g_oTarget = Obj;
    g_iTimeType = bGetTime ? 3 : 1;
    g_dtCurSel = dtCur;
    g_iCentOffset = 0;
    if (g_oCalendarCn == null) {
        g_oCalendarCn = window.createPopup();
    }
    InitCalendar(g_oCalendarCn);
    g_sCurPanelType = (g_iTimeType == 2 ? PT_SEC : PT_DAY);
    UpdatePanel();
    g_oCalendarCn.show(0, Obj.offsetHeight, 304, (bGetTime ? 290 : 270), Obj);
}

/************************************************/

/*
去除日期中的日期部分,留下年和月
*/
function RemoveDate(oThis) {
    if (Instr(oThis.value, "-")) {
        var sNewValue = strLeftBack(oThis.value, "-");
        if (Instr(sNewValue, "-"))
            oThis.value = sNewValue;
    }
}

function UpdatePanel() {
    var dtToday = new Date();
    if (g_dtCurSel == null || isNaN(g_dtCurSel)) {
        g_dtCurSel = dtToday;
    }
    var sVal = "";
    if ((g_iTimeType & 2) == 2) {
        sVal = g_dtCurSel.getHours();
        if (sVal < 10) sVal = "0" + sVal;
        g_oCalendarCn.document.all.PartH.innerText = sVal + "时";
        sVal = g_dtCurSel.getMinutes();
        if (sVal < 10) sVal = "0" + sVal;
        g_oCalendarCn.document.all.PartN.innerText = sVal + "分";
        sVal = g_dtCurSel.getSeconds();
        if (sVal < 10) sVal = "0" + sVal;
        g_oCalendarCn.document.all.PartS.innerText = sVal + "秒";
    }
    if ((g_iTimeType & 1) == 1) {
        sVal = g_dtCurSel.getMonth() + 1;
        if (sVal < 10) sVal = "0" + sVal;
        g_oCalendarCn.document.all.PartM.innerText = sVal + "月";
        sVal = g_dtCurSel.getDate();
        if (sVal < 10) sVal = "0" + sVal;
        g_oCalendarCn.document.all.PartD.innerText = sVal + "日";
        sVal = g_dtCurSel.getFullYear();
        g_oCalendarCn.document.all.PartY.innerText = sVal + "年";
    }
    var oPanel = g_oCalendarCn.document.all.Panel;
    if (g_oOldPart != null) {
        //g_oOldPart.style.border="";
        g_oOldPart.style.backgroundColor = "";
    }
    var iCur, iCurY, iToday, ixp, iyp;
    var sHtml, sExp, sSel;
    if (g_sCurPanelType == PT_YEAR) {
        g_oOldPart = g_oCalendarCn.document.all.PartY;

        iCur = g_dtCurSel.getFullYear();
        iToday = dtToday.getFullYear();
        sVal = iCur - (iCur % 5) - 25 + g_iYearOffset;
        if (sVal < 0) sVal = 0;
        sHtml = "<table style='width:100%;border:1px solid white;font-size:9pt;'>";
        for (ixp = 0; ixp < 8; ixp++) {
            sHtml += "<tr style='height:20px;' bgcolor='#EFEFEF' valign='middle' align='center'>";
            if (ixp == 0) {
                sHtml += "<td bgcolor='#CCCCCC' bcr='#CCCCCC' style='border:1 solid #CCCCCC;color:red;width:5%;font-family:Marlett;font-size:12pt;cursor:hand;' onclick='parent.Panel_Year(true)' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>5</td>";
            } else if (ixp == 7) {
                sHtml += "<td bgcolor='#CCCCCC' bcr='#CCCCCC' style='border:1 solid #CCCCCC;color:red;font-family:Marlett;font-size:12pt;cursor:hand;' onclick='parent.Panel_Year(false)' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)'>6</td>";
            } else if (ixp == 1) {
                sHtml += "<td rowSpan=6>&nbsp;</td>";
            }
            for (iyp = 0; iyp < 5; iyp++) {
                if (iCur == sVal) sSel = "background-color:#FF0000;color:#FFFFFF;";
                else if (iToday == sVal) sSel = "background-color:#45D045;";
                else sSel = "";
                sHtml += "<td onclick='parent.Panel_Click(" + sVal + ",\"" + g_sCurPanelType + "\",0)' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)' bcr='#EFEFEF' style='border:1 solid #EFEFEF;cursor:hand;" + sSel + (ixp == 0 ? "width:19%;" : "") + "'>" + sVal + "</td>";
                sVal++;
            }
            sHtml += "</tr>";
        }
    } else if (g_sCurPanelType == PT_MON) {
        g_oOldPart = g_oCalendarCn.document.all.PartM;

        iCur = g_dtCurSel.getMonth() + 1;
        iToday = dtToday.getMonth() + 1;
        sVal = 1;
        sHtml = "<table style='width:100%;height:50%;border:1px solid white;margin-top:20px;'>";
        iCurY = g_dtCurSel.getFullYear();
        for (ixp = 0; ixp < 2; ixp++) {
            sHtml += "<tr style='height:26px;' bgcolor='#EFEFEF' valign='middle' align='center'>";
            for (iyp = 0; iyp < 6; iyp++) {
                if (iCur == sVal) sSel = "background-color:#FF0000;color:#FFFFFF;";
                else if (iCurY == dtToday.getFullYear() && iToday == sVal) sSel = "background-color:#45D045;";
                else sSel = "";
                sHtml += "<td onclick='parent.Panel_Click(" + sVal + ",\"" + g_sCurPanelType + "\",0)' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)' bcr='#EFEFEF' style='border:1 solid #EFEFEF;cursor:hand;" + sSel + (ixp == 0 ? "width:16%;" : "") + "'>" + sVal + "<font color='#A0A0A0'>月</font></td>";
                sVal++;
            }
            sHtml += "</tr>";
            if (ixp == 0) sHtml += "<tr><td colspan=6 style='height:50px;background-color:#CCCCCC;'>&nbsp;</td></tr>";
        }
    } else if (g_sCurPanelType == PT_DAY) {
        g_oOldPart = g_oCalendarCn.document.all.PartD;

        var iWeek, iSign;
        iCurY = g_dtCurSel.getMonth();
        iCur = g_dtCurSel.getDate();
        iToday = dtToday.getDate();

        var dtFirst = new Date(g_dtCurSel.getFullYear(), g_dtCurSel.getMonth(), 1);
        iWeek = dtFirst.getDay();
        dtFirst.setDate(1 - iWeek);
        sHtml = "<table style='width:100%;border:1px solid white;'>" +
		"<tr style='font-size:9pt;height:18px;font-weight:bold;' bgcolor='#CCCCCC' valign='middle' align='center'><td style='width:14.29%;'>日</td><td style='width:14.29%;'>一</td><td style='width:14.29%;'>二</td><td style='width:14.29%;'>三</td><td style='width:14.29%;'>四</td><td style='width:14.29%;'>五</td><td style='width:14.29%;'>六</td></tr>";
        for (ixp = 0; ixp < 6; ixp++) {
            sHtml += "<tr style='font-size:10pt;height:18px;' bgcolor='#EFEFEF' valign='middle' align='center'>";
            for (iyp = 0; iyp < 7; iyp++) {
                sVal = dtFirst.getDate();
                if (iCurY != dtFirst.getMonth()) {
                    if (iyp == 0 || iyp == 6) sExp = "color:#FFAAAA;";
                    else sExp = "color:#CCCCCC";
                } else if (iyp == 0 || iyp == 6) {
                    sExp = "color:#FF2020;";
                } else {
                    sExp = "";
                }
                if (iCurY == dtFirst.getMonth() && iCur == sVal) {
                    sSel = "background-color:#FF0000;color:#FFFFFF;";
                    sExp = "";
                } else if (dtFirst.getMonth() == dtToday.getMonth() && iToday == sVal) {
                    sSel = "background-color:#45D045;";
                } else {
                    sSel = "";
                }
                iSign = 0;
                if (iCurY != dtFirst.getMonth()) {
                    if (iCurY == 11 && dtFirst.getMonth() == 0) iSign = 1;
                    else if (iCurY == 0 && dtFirst.getMonth() == 11) iSign = -1;
                    else if (dtFirst.getMonth() > iCurY) iSign = 1;
                    else if (dtFirst.getMonth() < iCurY) iSign = -1;
                }
                sHtml += "<td onclick='parent.Panel_Click(" + sVal + ",\"" + g_sCurPanelType + "\"," + iSign + ")' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)' bcr='#EFEFEF' style='border:1 solid #EFEFEF;cursor:hand;" + sSel + sExp + "'>" + sVal + "</td>";
                dtFirst.setDate(sVal + 1);
            }
        }
    } else if (g_sCurPanelType == PT_HOUR) {
        g_oOldPart = g_oCalendarCn.document.all.PartH;

        iCur = g_dtCurSel.getHours();
        iToday = dtToday.getHours();
        sVal = 0;
        sHtml = "<table style='width:100%;border:1px solid white;font-size:10pt;'>";
        for (ixp = 0; ixp < 6; ixp++) {
            if (ixp > 2) sExp = "#D8D8D8";
            else sExp = "#EFEFEF";
            sHtml += "<tr style='height:26px;' bgcolor='" + sExp + "' valign='middle' align='center'>";
            for (iyp = 0; iyp < 4; iyp++) {
                if (iCur == sVal) sSel = "background-color:#FF0000;color:#FFFFFF;";
                else if (iToday == sVal) sSel = "background-color:#45D045;";
                else sSel = "";

                sHtml += "<td onclick='parent.Panel_Click(" + sVal + ",\"" + g_sCurPanelType + "\",0)'' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)' bcr='" + sExp + "' style='border:1 solid " + sExp + ";cursor:hand;" + sSel + (ixp == 0 ? "width:25%;" : "") + "'>" + (sVal < 10 ? "0" : "") + sVal + "<font color='#A0A0A0'>时</font></td>";
                sVal++;
            }
        }
    } else if (g_sCurPanelType == PT_MINU) {
        g_oOldPart = g_oCalendarCn.document.all.PartN;

        iCur = g_dtCurSel.getMinutes();
        iToday = dtToday.getMinutes();
        sVal = 0;
        sHtml = "<table style='width:100%;border:1px solid white;font-size:10pt;'>";
        for (ixp = 0; ixp < 6; ixp++) {
            sHtml += "<tr style='height:26px;' bgcolor='#EFEFEF' valign='middle' align='center'>";
            for (iyp = 0; iyp < 3; iyp++) {
                if (iCur == sVal) sSel = "background-color:#FF0000;color:#FFFFFF;";
                else if (iToday == sVal) sSel = "background-color:#45D045;";
                else sSel = "";

                sHtml += "<td onclick='parent.Panel_Click(" + sVal + ",\"" + g_sCurPanelType + "\",0)' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)' bcr='#EFEFEF' style='border:1 solid #EFEFEF;cursor:hand;" + sSel + (ixp == 0 ? "width:33.3%;" : "") + "'>" + (sVal < 10 ? "0" : "") + sVal + "<font color='#A0A0A0'>分</font></td>";
                if (iyp == 0) sVal += 5;
                else if (iyp == 1) sVal += 4;
                else sVal += 1;
            }
        }
    } else if (g_sCurPanelType == PT_SEC) {
        g_oOldPart = g_oCalendarCn.document.all.PartS;

        iCur = g_dtCurSel.getSeconds();
        iToday = dtToday.getSeconds();
        sVal = 0;
        sHtml = "<table style='width:100%;border:1px solid white;font-size:10pt;'>";
        for (ixp = 0; ixp < 6; ixp++) {
            sHtml += "<tr style='height:26px;' bgcolor='#EFEFEF' valign='middle' align='center'>";
            for (iyp = 0; iyp < 3; iyp++) {
                if (iCur == sVal) sSel = "background-color:#FF0000;color:#FFFFFF;";
                else if (iToday == sVal) sSel = "background-color:#45D045;";
                else sSel = "";

                sHtml += "<td onclick='parent.Panel_Click(" + sVal + ",\"" + g_sCurPanelType + "\",0)' onmouseover='parent.MMObj(this,1)' onmouseout='parent.MMObj(this,0)' bcr='#EFEFEF' style='border:1 solid #EFEFEF;cursor:hand;" + sSel + (ixp == 0 ? "width:33.3%;" : "") + "'>" + (sVal < 10 ? "0" : "") + sVal + "<font color='#A0A0A0'>秒</font></td>";
                if (iyp == 0) sVal += 5;
                else if (iyp == 1) sVal += 4;
                else sVal += 1;
            }
        }
    }
    sHtml += "</table>";
    oPanel.innerHTML = sHtml;

    if (g_oOldPart != null) {
        //g_oOldPart.style.border="1 solid red";
        g_oOldPart.style.backgroundColor = "#FF0000";
    }
}


//选择日期
//sYear 年份
//sMonth 月份
//sSeletedDays 默认选中的日期 (日期以“；”隔开，直接采用数字表示天数即可，例如希望选中1到3号，则传入 1;2;3即可)
//sFormatType 返回日期的格式化形式  yyyy 代表年 MM 代表月 dd 代表日
//bIncludeWeek 返回的日期格式是否包含星期数据，为true时，包含星期！例如：返回如下格式的数据：2011-07-01(星期五);2011-07-02(星期六)
//返回选中的日期
function SelectDate(sYear, sMonth, sSelectedDays, sFormatType, bIncludeWeek) {
    if (!sYear || !sMonth) {
        alert("必须输入年份以及月份");
        return false;
    }
    var aParam = new Array();
    aParam[0] = sYear;
    aParam[1] = sMonth;
    aParam[2] = sSelectedDays;
    aParam[3] = sFormatType;
    aParam[4] = bIncludeWeek;
    var sUrl = _MyWebsite.CommonElementsFolderUrl() + "/DateTimeCtrl/SelectDate.aspx";
    var sReturn = window.showModalDialog(sUrl, aParam, "dialogHeight=320px;dialogWidth=350px;status=0;help=0;scrollbars=no; resizable=1");
    return sReturn;
}
    